package com.bs.bszhsq.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.bszhsq.entity.Kehu;
import com.bs.bszhsq.exception.ServiceException;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import com.bs.bszhsq.utils.R;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.*;

import com.bs.bszhsq.service.IFixService;
import com.bs.bszhsq.entity.Fix;


import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 在线报修 前端控制器
 * </p>
 *
 * @author 章宇光
 * @since 2024-03-24
 */
@RestController
@RequestMapping("/fix")
@Api(tags = "社区维修接口")
        public class FixController {
    //service注入
@Resource
private IFixService fixService;


@PostMapping
@ApiOperation(value = "新增")
public R save(@RequestBody Fix fix){
        if (BeanUtil.isEmpty(fix)){
                throw new ServiceException("500","传入参数不完整！");
        }
        fix.setAddtime(LocalDateTime.now());
        fix.setOrderId(RandomUtil.randomNumbers(10));
        boolean save= fixService.save(fix);
        return R.write(save);
        }


@PostMapping("/delete")
@ApiOperation(value = "删除")
public R delete(@RequestBody Long id){
        boolean b=fixService.removeById(id);
        return R.write(b);
        }

@PostMapping("/update")
@ApiOperation(value = "修改")
public R update(@RequestBody Fix fix) {
        boolean update = fixService.updateById(fix);
        return R.write(update);
        }


@PostMapping("/deletebatch")
@ApiOperation(value = "批量删除")
public R deletebatch(@RequestBody List<Long> ids) {
        boolean b = fixService.removeByIds(ids);
        return R.write(b);
        }


@GetMapping("/page")
@ApiOperation(value = "报修记录分页查询")
public R page(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam(defaultValue = "") String name) {
        IPage<Fix> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Fix> qw = new QueryWrapper<>();
        if (!"".equals(name)) {
                qw.like("name", name);
        }
        qw.orderByDesc("id");
        IPage<Fix> fixIPage = fixService.page(page, qw);
        return R.success(fixIPage);
        }


        @GetMapping("/export")
        @ApiOperation(value = "报修信息导出")
        public R export(HttpServletResponse response) throws Exception {
                //先获取全部数据
                List<Fix> list = fixService.list();
                //写出到浏览器
                ExcelWriter writer = ExcelUtil.getWriter(true);
                //定义别名
                writer.addHeaderAlias("id","ID");
                writer.addHeaderAlias("orderId","报修单号");
                writer.addHeaderAlias("name","报修名称");
                writer.addHeaderAlias("door","门牌号");
                writer.addHeaderAlias("content","报修内容");
                writer.addHeaderAlias("fixDate","报修日期");
                writer.addHeaderAlias("note","报修备注");
                writer.addHeaderAlias("username","姓名");
                writer.addHeaderAlias("phone","手机");
                writer.addHeaderAlias("addtime","创建时间");

                //写入数据
                writer.write(list,true);
                //设置浏览器响应格式
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                String fileName = URLEncoder.encode("报修信息", "UTF-8");
//test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
                response.setHeader("Content-Disposition","attachment;filename="+fileName+".xls");
                ServletOutputStream outputStream = response.getOutputStream();
                writer.flush(outputStream,true);
                outputStream.close();
                writer.close();
                return R.success();
        }

        @PostMapping("/import")
        @ApiOperation(value = "导入报修信息")
        public R imp(MultipartFile file) throws Exception{
                InputStream inputStream = file.getInputStream();
                ExcelReader reader = ExcelUtil.getReader(inputStream);
                Map<String,String> map=new HashMap<>();
                map.put("报修单号","orderId");
                map.put("报修名称","name");
                map.put("门牌号","door");
                map.put("报修内容","content");
                map.put("报修日期","fixDate");
                map.put("报修备注","note");
                map.put("姓名","username");
                map.put("手机","phone");
                reader.setHeaderAlias(map);
                List<Fix> fixes = reader.readAll(Fix.class);
                for (Fix fix : fixes) {
                        fix.setAddtime(LocalDateTime.now());
                }
                boolean b = fixService.saveBatch(fixes);
                System.out.println("文件导入"+b);
                return R.write(b);
        }




        }
